import CryptoJS from "crypto-js";

const KEY = CryptoJS.enc.Utf8.parse("QueYouQiShiToApp");
const IV = CryptoJS.enc.Utf8.parse("AppToQueYouQiShi");

// 加密
export function encrypt(text: string): string {
  const srcs = CryptoJS.enc.Utf8.parse(text);
  const encrypted = CryptoJS.AES.encrypt(srcs, KEY, { iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.ciphertext.toString().toUpperCase();
}

// 解密
export function decrypt(text: string): string {
  const encryptedHexStr = CryptoJS.enc.Hex.parse(text);
  const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  const decrypt = CryptoJS.AES.decrypt(str, KEY, {
    iv: IV,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}
